package test;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

public class SSH {
	  private String hadoop_conf=null;
	  private String hive_conf=null;
	  
	  public int checkSSH(String hostname,String username,String password) throws Exception{
		  System.out.println(hostname+" "+username+" ");
		  int port2=22;
			try {
				Socket s=new Socket(hostname,port2);
				s.close();
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new Exception("login fail,check your username or password2!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				port2=10001;
			}
		
			System.out.println(hostname+" "+port2+" ");
		  Connection conn = new Connection(hostname,port2);
			try {
				conn.connect();
				boolean isAuthenticated = conn.authenticateWithPassword(username,
						password);
				if (isAuthenticated == false) {
					System.err.println("login fail,check your username or password3!");
					throw new Exception("login fail,check your username or password4!");
				}
				Session session = conn.openSession();
				String cmd =new String("env |grep HADOOP_CONF_DIR".getBytes(),"UTF-8");
				System.out.println(cmd);
				session.execCommand(cmd);
				int readLen;
				byte buf_result[] = new byte[1024];

				while ((readLen = session.getStdout().read(buf_result, 0, 1024)) != -1) { 
					this.hadoop_conf=new String(buf_result, 0, readLen); 
					//System.out.print(new String(buf_result, 0, readLen));
				}
				if(this.hadoop_conf!=null){
					this.hadoop_conf=this.hadoop_conf.substring(0, this.hadoop_conf.lastIndexOf("\n"));
					this.hadoop_conf=this.hadoop_conf.substring("HADOOP_CONF_DIR=".length());
				}

				session.close();
				System.out.println(hadoop_conf);
				
				 session = conn.openSession();
//				 cmd = "env |grep HIVE_CONF_DIR";
				 cmd =new String("env |grep HIVE_CONF_DIR".getBytes(),"UTF-8");
				session.execCommand(cmd);

				while ((readLen = session.getStdout().read(buf_result, 0, 1024)) != -1) { // ��û�ж�ȡ��ʱ�������ȡ
					this.hive_conf=new String(buf_result, 0, readLen); 
				}
				if(this.hive_conf!=null){
					this.hive_conf=this.hive_conf.substring(0, this.hive_conf.lastIndexOf("\n"));
					this.hive_conf=this.hive_conf.substring("HIVE_CONF_DIR=".length());
				}
				System.out.println(this.hive_conf);
				session.close();
				conn.close();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new Exception("login fail,check your username or password5!");
//				e.printStackTrace();
			}
			return 1;
	  }
	 
	public static void main(String[] args) {
		String hostname="172.23.113.21";
//		String hostname="10.232.128.65";
		int port=10000;
		try {
			new SSH().checkSSH(hostname, "puyuan", "tbpy62683");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			System.out.println("bb");
			Socket s=new Socket(hostname,port);
			System.out.println("aa");
			s.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("ok!!");
	}
}
